﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using bdnt_pets_system.Tools;
using bdnt_pets_system_Entity;
using System.Windows.Data;
using bdnt_pets_system.Resources;

namespace bdnt_pets_system.PageViews.DataManager
{
    public partial class StationManagerControl : UserControl
    {
        DataServiceReference.DataServiceClient dataClient;
        int pageSize = SessionManager.PageSize;
        int currentPage = 1;

        public StationManagerControl()
        {
            InitializeComponent();
            dataClient = new DataServiceReference.DataServiceClient();
            InitData();
            InitEvent();
            QueryData();
        }

        Tbl_Station CuTbl_Station
        {
            get { return this.DataContext as Tbl_Station; }
            set { this.DataContext = value; }
        }

        //默认查询条件
        Tbl_Station selectStation = new Tbl_Station()
        {
            IsValidate = false,
            StationNo="请输入工位编号",
            StationName="请输入工位名称"
        };

        void InitData()
        {
            tbStationNo.Foreground = tbStationName.Foreground = new SolidColorBrush(Colors.Gray);
            tbStationNo.SetBinding(TextBox.TextProperty, new Binding("StationNo") { Mode = BindingMode.TwoWay });
            tbStationNo.Tag = selectStation.StationNo;
            tbStationName.SetBinding(TextBox.TextProperty, new Binding("StationName") { Mode = BindingMode.TwoWay });
            tbStationName.Tag = selectStation.StationName;

            CuTbl_Station = new Tbl_Station()
            {
                IsValidate = false,
                StationNo = selectStation.StationNo,
                StationName = selectStation.StationName
            };
        }

        void InitEvent()
        {
            btnNew.Click += new RoutedEventHandler(btnNew_Click);
            btnSelect.Click += new RoutedEventHandler(btnSelect_Click);
            dataClient.GetStationListByConditionCompleted += new System.EventHandler<DataServiceReference.GetStationListByConditionCompletedEventArgs>(dataClient_GetStationListByConditionCompleted);
            dataClient.GetStationListByConditionCountCompleted += new System.EventHandler<DataServiceReference.GetStationListByConditionCountCompletedEventArgs>(dataClient_GetStationListByConditionCountCompleted);
            dataClient.DeleteCuStationCompleted += new System.EventHandler<DataServiceReference.DeleteCuStationCompletedEventArgs>(dataClient_DeleteCuStationCompleted);
            StationPageC.dpPage.PageIndexChanged += new System.EventHandler<System.EventArgs>(dpPage_PageIndexChanged);
        }

        #region 查询与分页

        //数据库查询条件对象
        Tbl_Station selecV = new Tbl_Station();

        void QueryData()
        {
            selecV = new Tbl_Station()
            {
                IsValidate = false,
                StationNo = CuTbl_Station.StationNo == selectStation.StationNo ? "" : CuTbl_Station.StationNo,
                StationName = CuTbl_Station.StationName == selectStation.StationName ? "" : CuTbl_Station.StationName
            };
            dataClient.GetStationListByConditionCountAsync(selecV);
        }

        void dataClient_GetStationListByConditionCountCompleted(object sender, DataServiceReference.GetStationListByConditionCountCompletedEventArgs e)
        {
            StationPageC.LoadPages(e.Result, pageSize);
        }

        void dpPage_PageIndexChanged(object sender, System.EventArgs e)
        {
            currentPage = StationPageC.PageIndex + 1;
            dataClient.GetStationListByConditionAsync(selecV, pageSize, currentPage);
        }

        void dataClient_GetStationListByConditionCompleted(object sender, DataServiceReference.GetStationListByConditionCompletedEventArgs e)
        {
            gdList.ItemsSource = e.Result;
        }
        #endregion

        void dataClient_DeleteCuStationCompleted(object sender, DataServiceReference.DeleteCuStationCompletedEventArgs e)
        {
            if (e.Result)
                QueryData();
        }

        void btnSelect_Click(object sender, RoutedEventArgs e)
        {
            QueryData();
        }

        void btnNew_Click(object sender, RoutedEventArgs e)
        {
            var cupage = new NewStationWindow(new Tbl_Station());
            cupage.Show();
            cupage.Closed += new EventHandler(cupage_Closed);
        }

        void cupage_Closed(object sender, EventArgs e)
        {
            if ((sender as ChildWindow).DialogResult == true)
                QueryData();
        }

        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            var cuObj = (sender as HyperlinkButton).Tag as Tbl_Station;
            var cupage = new NewStationWindow(cuObj);
            cupage.Show();
            cupage.Closed += new EventHandler(cupage_Closed);
        }

        private void HyperlinkButton_Click_1(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show(MyStrings.qdyscm, MyStrings.ts, MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
                return;
            var cuObj = (sender as HyperlinkButton).Tag as Tbl_Station;
            dataClient.DeleteCuFailureInfoAsync(cuObj.Id);
        }
    }
}
